import 'dart:io';
import 'package:flutter/material.dart';
import 'package:geomancy/enums/index.dart';

// 是通过哪里进入相册的, register:注册 edit:修改个人头像 comment:评论消息 group:群组消息 single:私聊消息 poll:创建投票 report:举报
enum EnterGalleryPageType {register, edit, comment, group, single, poll, feedback, report}

class GalleryProvider extends ChangeNotifier {
  //全局设置图片的裁剪比例
  double? _cropAspectRatio;
  //全局存储裁剪后的图片
  File? _cropFile;
  EnterGalleryPageType? _enterGalleryPageType;

  //全局存储当前相册的所有图片和视频：AssetEntity 和 File 类型
  List<dynamic> _allGalleryMediaList = <dynamic>[];
  //全局设置最多选择几个图片和视频
  int _maxLength = 9;
  //全局存储临时选中的图片和视频
  List<dynamic> _tmpSelectedList = <dynamic>[];
  //全局存储选中的图片和视频 AssetEntity,File
  List<dynamic> _selectedList = <dynamic>[];
  //相册使用场景
  GalleryScene _scene = GalleryScene.common;

  //重置所有数据
  reset() {
    _cropAspectRatio = null;
    _cropFile = null;
    _enterGalleryPageType = null;
    _maxLength = 9;
    _tmpSelectedList = [];
    _selectedList = [];
    _scene = GalleryScene.common;
    // notifyListeners();
  }


  double? get getCropAspectRatio => _cropAspectRatio;
  File? get getCropFile => _cropFile;
  EnterGalleryPageType? get getEnterGalleryPageType => _enterGalleryPageType;

  List<dynamic> get getAllGalleryMediaList => _allGalleryMediaList;
  int get getMaxLength => _maxLength;
  List<dynamic> get getTmpSelectedList => _tmpSelectedList;
  List<dynamic> get getSelectedList => _selectedList;
  GalleryScene get getGalleryScene => _scene;

  set setGalleryScene(GalleryScene value) {
    _scene = value;
    notifyListeners();
  }

  set setCropAspectRatio(double? value) {
    _cropAspectRatio = value;
    notifyListeners();
  }
  set setCropFile(File? value) {
    _cropFile = value;
    notifyListeners();
  }

  set setEnterGalleryPageType(EnterGalleryPageType? value) {
    _enterGalleryPageType = value;
    notifyListeners();
  }

  set setAllGalleryMediaList(List<dynamic> list) {
    _allGalleryMediaList = list;
    notifyListeners();
  }
  addAllGalleryMediaList(dynamic value) {
    _allGalleryMediaList.insert(0, value);
    notifyListeners();
  }
  replaceAllGalleryMediaList(int index, dynamic value) {
    if (_allGalleryMediaList.length > index) {
      _allGalleryMediaList[index] = value;
      // _allGalleryMediaList.removeAt(index);
      // _allGalleryMediaList.insert(index, value);
      notifyListeners();
    }
  }
  set setMaxLength(int value) {
    _maxLength = value;
    notifyListeners();
  }
  addTmpSelectedList(dynamic value) {
    if (_tmpSelectedList.contains(value)) {
      removeTmpSelectedList(value);
    } else {
      _tmpSelectedList.add(value);
    }
    notifyListeners();
  }
  updateTmpSelectedList(int index, dynamic value) {
    if (_tmpSelectedList.length > index) {
      _tmpSelectedList[index] = value;
    } else {
      _tmpSelectedList.add(value);
    }
    notifyListeners();
  }
  removeTmpSelectedList(dynamic value) {
    _tmpSelectedList.remove(value);
    notifyListeners();
  }
  setTmpSelectedList(List<dynamic>? list) {
    if (list == null  || list.isEmpty) {
      _tmpSelectedList = [];
    } else {
      _tmpSelectedList = list;
    }
    notifyListeners();
  }
  setTmpSelectedListEmpty() {
    _tmpSelectedList = [];
    notifyListeners();
  }
  setSelectedList(List<dynamic> list) {
    _selectedList = list;
    notifyListeners();
  }
  setSelectedListEmpty() {
    _selectedList = [];
    notifyListeners();
  }
}